#include <glib.h>
#include <glib/gprintf.h>
#include "common.h"

static void _dummy(const gchar *log_domain,
                     GLogLevelFlags log_level,
                     const gchar *message,
                     gpointer user_data )
{
	g_printf("%s\n", message);
	return ;
}

static int arry[10] = {0};
static int b = 2;
G_LOCK_DEFINE(mutex);

gpointer thread1(gpointer data)
{
	while ( TRUE ) {
		g_bit_lock(&b, 1);
		g_debug("thread one b(%d)", b);
		arry[0] += 1;
		g_usleep(1000*1000);
		g_bit_unlock(&b, 1);
	}
}

gpointer thread2(gpointer data)
{
	while ( TRUE ) {
		g_bit_lock(&b, 1);
		g_debug("thread two: %d", arry[0]);
		g_bit_unlock(&b, 1);
	}
}

gpointer thread3(gpointer data)
{
	while ( TRUE ) {
		g_bit_lock(&b, 1);
		g_debug("thread three: %d", arry[0]);
		g_bit_unlock(&b, 1);
	}
}

int main(int argc,char *argv[])
{
	GThread *t1 = g_thread_new("thread1", thread1, GSIZE_TO_POINTER(1));
	GThread *t2 = g_thread_new("thread2", thread2, GSIZE_TO_POINTER(2));
	GThread *t3 = g_thread_new("thread3", thread3, GSIZE_TO_POINTER(3));

	g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,  _dummy, NULL);

	g_thread_join(t1);
	g_thread_join(t2);
	g_thread_join(t3);
	return 0;
} 
